home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Is this ok: *pointer++ = value ??
- Date: Sat, 06 Jan 1996 14:13:37 GMT
- Organization: Netcom
- Message-ID: <30ee81fc.41548928@nntp.ix.netcom.com>
- References: <4cklvv$nmm@alcor.usc.edu>
- NNTP-Posting-Host: ix-dc13-29.ix.netcom.com
- X-NETCOM-Date: Sat Jan 06 6:13:27 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- wawda@alcor.usc.edu (Abu Wawda) wrote:
-
- |>I tried the following on my C compiler and it worked as expected:
- |>
- |> int i;
- |> char *pointer;
- |>
- |> pointer = (char *) malloc(50);
- |> for (i=0; i<50; i++) *pointer++ = i;
- |>
- |>My only problem is why? I mean you aren't allowed to do:
- |>
- |> int i,p;
- |>
- |> for (i=0; i<50; i++) p++ = i;
- |>
- |>Like you aren't supposed to be put anything like x+5 or some other
- |>unsolved quantity on the left side for assignment. However, is this
- an
- |>exception for pointers? I can understand doing it backwards as
- |>perfectly legal:
- |>
- |> int x;
- |>
- |> x = *pointer++;
- |>
- |>but not:
- |>
- |> *pointer++ = x;
- |>
- |>I would appreciate any hints. Thanks you.
-
- The problem is that you're thinking in terms of "you aren't supposed
- to be put anything like x+5 or some other unsolved quantity on the
- left side for assignment." This rule is too imprecise to work with in
- programming.
-
- There are specific rules for what may be on the left hand side of an
- assignment. The left operand must be a modifiable lvalue. If p is a
- pointer to something that is not const, *p is a modifiable lvalue.
- Since pointer is a pointer to int, pointer++ is also a pointer to int
- and *(pointer++) (which is equivalent to *pointer++) is a modifiable
- lvalue.
-
- Michael M Rubenstein
-